/**
 * 
 */
package fr.rcp.file.explorer.views;

import java.io.File;
import java.net.URL;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;

import fr.rcp.file.explorer.controleur.Controleur;
import fr.rcp.file.explorer.controleur.DeleteAction;
import fr.rcp.file.explorer.controleur.ExecuteAction;
import fr.rcp.file.explorer.controleur.OpenAction;
import fr.rcp.file.explorer.controleur.PropertyAction;
import fr.rcp.file.explorer.controleur.RenameAction;
import fr.rcp.file.explorer.provider.TableContentProvider;

/**
 * @author fred
 * 
 */
public class Console extends ViewPart {
	public static final String ID = "fr.rcp.file.explorer.views.console";
	private TableViewer tableViewer;

	/**
	 * @return the tableViewer
	 */
	public TableViewer getTableViewer() {
		return tableViewer;
	}

	/**
	 * @param tableViewer
	 *            the tableViewer to set
	 */
	public void setTableViewer(TableViewer tableViewer) {
		this.tableViewer = tableViewer;
	}

	public void createPartControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		container.setLayout(layout);

		tableViewer = new TableViewer(container, SWT.BORDER
				| SWT.FULL_SELECTION);
		tableViewer.setContentProvider(new TableContentProvider());
		TableViewerColumn name = new TableViewerColumn(tableViewer, SWT.NONE);
		name.getColumn().setText("Name");
		name.getColumn().setWidth(300);

		name.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public Image getImage(Object element) {
				if (((File) element).isDirectory()) {
					Bundle bundle = FrameworkUtil.getBundle(Console.class);
					URL url = FileLocator.find(bundle, new Path(
							"icons/folder_48.png"), null);
					ImageDescriptor image = ImageDescriptor.createFromURL(url);
					return image.createImage();
				} else {
					String name = ((File) element).getName();
					String ext = "extension";
					if (name.contains(".")
							&& name.substring(name.lastIndexOf(".")) != null)
						ext = name.substring(name.lastIndexOf("."));
					String file_icone = "";
					switch (ext) {
					case ".txt":
						file_icone = "icons/file_48.png";
						break;
					case ".exe":
						file_icone = "icons/exe_48.png";
						break;
					case ".html":
						file_icone = "icons/net_48.png";
						break;
					case ".xhtml":
						file_icone = "icons/net_48.png";
						break;
					case ".htm":
						file_icone = "icons/net_48.png";
						break;
					default:
						file_icone = "icons/unknown_48.png";
						break;
					}
					Bundle bundle = FrameworkUtil.getBundle(Console.class);
					URL url = FileLocator.find(bundle, new Path(file_icone),
							null);
					ImageDescriptor image = ImageDescriptor.createFromURL(url);
					return image.createImage();
				}
			}

			@Override
			public String getText(Object element) {
				return ((File) element).getName();
			}
		});

		TableViewerColumn size = new TableViewerColumn(tableViewer, SWT.RIGHT);
		size.getColumn().setText("Size");
		size.getColumn().setWidth(300);
		size.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(Object element) {
				if (((File) element).isFile())
					return String.valueOf(((File) element).length());
				else
					return null;
			}
		});

		TableViewerColumn access = new TableViewerColumn(tableViewer, SWT.RIGHT);
		access.getColumn().setText("Access");
		access.getColumn().setWidth(300);
		access.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(Object element) {
				String access = "";
				if (((File) element).canRead())
					access += "r ";
				if (((File) element).canWrite())
					access += "w ";
				if (((File) element).canExecute())
					access += "x ";
				return access;
			}
		});

		GridData data = new GridData(GridData.FILL_BOTH);
		tableViewer.getTable().setLayoutData(data);
		tableViewer.getTable().setHeaderVisible(true);
		Controleur.getIntance().setTableViewer(tableViewer);
		tableViewer.setInput(new File("C:\\"));
		tableViewer.addSelectionChangedListener(Controleur.getIntance());
		tableViewer.addDoubleClickListener(Controleur.getIntance());
		
		/*
		 * Menu contextuel
		 */
		MenuManager contextMenu = new MenuManager();
		tableViewer.getTable().setMenu(
				contextMenu.createContextMenu(tableViewer.getTable()));
		contextMenu.add(new OpenAction(this, "Ouvrir"));
		contextMenu.add(new ExecuteAction(this, "Executer"));
		contextMenu.add(new RenameAction(this, "Renommer"));
		contextMenu.add(new DeleteAction(this, "Supprimer"));
		contextMenu.add(new PropertyAction(this, "Propriete"));

	}

	public void setFocus() {

	}
}
